<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <link href="../libs/mui.min.css" rel="stylesheet"/>
    <link rel="stylesheet" type="text/css" href="../libs/app.css"/>
    <link href="../css/main.css" rel="stylesheet"/>
    <link href="../css/enter.css" rel="stylesheet"/>
</head>

<body>
<div class="head">
    <p>登录</p>
</div>
<div class="context">
    <div><input class="number" type="tel" maxlength="11" placeholder="请输入手机号码">
        <img class="img" src="../images/suo.png" alt="">
    </div>
    <div><input class="password" type="password" placeholder="请输入登录密码">
        <img class="img" src="../images/photo-num.png" alt="">
    </div>
    <p class="tip">
        <a id="forget">忘记密码</a>
        <a id="zhuce">注册账号</a>
    </p>
    <div>
        <button class="sign">登录</button>
    </div>
    <div id="oauth"></div>
    <!--
    <div class="xia">
        <div class="di">
            <img class="di-img" src="../images/line.png" alt="">
        </div>
        <p>其他登录方式</p>
        <div class="other">
            <div id="qq" class="qq">
                <img src="../images/enter-qq.png" alt="">
                <p>QQ</p>
            </div>
            <div id="wechat" class="wx">
                <img src="../images/enter-wei.png" alt="">
                <p style="margin-top: 5%">微信</p>
            </div>
        </div>
    </div>
    -->

</div>
<script src="../libs/mui.min.js"></script>
<script src="../js/app.js"></script>
<script src="../js/mui.enterfocus.js"></script>
<script src="../js/jquery-3.1.1.min.js"></script>
<script src="../js/url.js"></script>
<script>
    localStorage.quick = 0;

    var wechatAppid = 'wx9ca2adaf47ff2ab1',
        openUrlSuccessUrl = encodeURIComponent(ur + 'web/html/enter.html'),
        url_Parse = urlParse(); // 获取微信登录返回后的code

    var wechatLogin = function () {
        localStorage.quick = 1;
        var openUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?' +
            'appid=' + wechatAppid +
            '&redirect_uri=' + openUrlSuccessUrl + // 授权后重定向的回调链接地址，请使用urlencode对链接进行处理
            '&response_type=code' +
            '&scope=snsapi_userinfo' + // snsapi_userinfo （弹出授权页面，可通过openid拿到昵称、性别、所在地。并且，即使在未关注的情况下，只要用户授权，也能获取其信息）
            '&state=STATE#wechat_redirect';
        window.location.href = openUrl;
    }

    var backCode = url_Parse.code ? url_Parse.code : null;
    // code作为换取access_token的票据
    if (backCode) {
        localStorage.quick = 1; // 快速登录
        console.log('backCode', backCode);
        localStorage.removeItem('wechatuser');
        $.ajax({
            type: "get",
            url: ur + "rest/wechatuser",
            headers: {
                'X-token': localStorage.token
            },
            data: {
                code: backCode
            },
            dataType: 'json',
            success: function (data) {
                console.log(data.meta.code)
                if (data.meta.code == 'S002') {
                    console.log('wechatuser', data.data);
                    /**
                     * openId：支付用
                     * unionId：登录用
                     */
                    localStorage.setItem('wechatuser', JSON.stringify(data.data));
                    // 调用快速登录接口
                    localStorage.setItem('openid', data.data.unionId);
                    localStorage.setItem('trueOpenId', data.data.openId);
                    localStorage.setItem('type', 'WECHAT');
                    quickLogin(data.data.unionId, 'WECHAT');
                } else {

                }
            }
        });
    } else {
        wechatLogin();
    }

    (function (mui, doc) {

        $(function () {

            // 点击登录
            $('.sign').on('tap', function () {
                if ($('.number').val() == '') {
                    mui.alert('请输入手机号码', '提示');
                } else if ($('.password').val() == '') {
                    mui.alert('请输入登录密码', '提示');
                } else {
                    $.ajax({
                        url: ur + 'rest/appUserController/login',
                        type: 'POST',
                        dataType: 'json',
                        data: {
                            "phone": $('.number').val(),
                            "pass": $('.password').val()
                        }
                    }).done(function (data) {
                        if (data.meta.code == 'S002') {
                            localStorage.token = data.data.token;
                            localStorage.loadphone = $('.number').val();
                            // localStorage.setItem('voice', 'false');
                            console.log(localStorage.token);
                            if (localStorage.quick == 0) {
                                mui.toast('欢迎来到小马时贷');
                                href('./head-foot-demo.html#/home');
                            } else {
                                bindUser();
                                href('./head-foot-demo.html#/home');
                            }
                        } else if (data.meta.code == 'E002') {
                            mui.toast('用户名或密码错误');
                        }
                    })
                }
            });
        });

        $(function () {
            $('#zhuce').on('tap', function () {
                href('login.html');
            });
            /*$('#qq').on('tap',function(){
             href('login.html','login.html')
             });
             $('#wechat').on('tap',function(){
             href('login.html','login.html')
             });*/
            $('#forget').on('tap', function () {
                href('forget.html');
            })
        });
    })(mui, document);

    //快速登录
    function quickLogin(id, type) {
        console.log('id, type', id, type)
        $.ajax({
            url: ur + 'rest/appUserController/quick-login',
            type: 'POST',
            dataType: 'json',
            data: {
                "openid": id ? id : localStorage.openid,
                "type": type ? type : localStorage.type
            },
            success: function (data) {
                if (data.meta.code == 'E004') {
                    href('login.html');
                } else if (data.meta.code == 'S002') {
                    localStorage.token = data.data.token;
                    href('head-foot-demo.html');
                    mui.toast('欢迎来到小马时贷');
                } else {
                    mui.toast(data.meta.message);
                }
            }
        })
    }

    //	绑定快速登录
    function bindUser() {
        $.ajax({
            url: ur + 'rest/appUserController/bind-user',
            type: 'POST',
            dataType: 'json',
            headers: {
                "X-Token": localStorage.token
            },
            data: {
                "openid": localStorage.openid,
                "params": '',
                "type": localStorage.type
            },
            success: function (data) {
                if (data.meta.code == 'S002') {
                    mui.back();
                } else {
                    mui.toast('系统异常');
                }
            },
            error: function (e) {
            }
        })
    }
</script>
</body>

</html>